package com.ibis.debuglistwidget;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;

/**
 * Created by dev on 8/2/13.
 */
public class LogActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActionBar actionBar;
        actionBar = getActionBar();
        actionBar.setSubtitle("Logs");
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tabLog = actionBar.newTab();
        ActionBar.Tab log = tabLog.setText("Log")
                .setIcon(R.drawable.ic_launcher)
                .setContentDescription("List Log")
                .setTabListener(
                        new TabListener<LogListFragment>(this, R.id.fragmentContainer, LogListFragment.class));

        actionBar.addTab(tabLog);

        setContentView(R.layout.activity_main);
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);

//        StringBuilder builder = new StringBuilder();

//        builder.append("n" + sharedPrefs.getString("refreshFrequency", "4000"));

//        TextView settingsTextView = (TextView) findViewById(R.id.settings_text_view);
//        settingsTextView.setText(builder.toString());
    }


    @Override
    protected void onNewIntent(Intent intent) {
        // Log.i("alogcat", "new intent: " + intent);
        if (intent == null) {
            return;
        }
        if (intent.getAction() == null) {
            return;
        }
        this.setIntent(intent);
        if (intent.getAction().equals(com.ibis.debuglistwidget.Intent.START_INTENT)) {
            com.ibis.debuglistwidget.Intent.handleExtras(this, intent);
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        menu.add(Menu.NONE, 0, 0, "Show current settings");

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case 0:

                startActivity(new Intent(this, PrefsActivity.class));

                return true;

        }

        return false;
    }

    public static class TabListener<T extends ListFragment>
            implements ActionBar.TabListener {

        private LogListFragment fragment;
        private Activity activity;
        private Class<T> fragmentClass;
        private int fragmentContainer;

        public TabListener(Activity activity, int fragmentContainer,
                           Class<T> fragmentClass) {

            this.activity = activity;
            this.fragmentContainer = fragmentContainer;
            this.fragmentClass = fragmentClass;
        }

        // Called when a new tab has been selected
        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            if (fragment == null) {
                String fragmentName = fragmentClass.getName();
                fragment =
                        (LogListFragment)Fragment.instantiate(activity, fragmentName);
                ft.add(fragmentContainer, fragment, null);
//                fragment.setFragmentText(tab.getText());
            } else {
                ft.attach(fragment);
            }
        }

        // Called on the currently selected tab when a different tag is
        // selected.
        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
            if (fragment != null) {
                ft.detach(fragment);
            }
        }

        // Called when the selected tab is selected.
        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // TODO React to a selected tab being selected again.
        }

    }
}
